Imports System
Imports System.Collections.Generic
Imports System.Runtime.InteropServices
Imports System.Web.UI
Imports System.Web.UI.WebControls.WebParts
Imports Microsoft.SharePoint.WebPartPages
Imports Microsoft.SharePoint
Imports System.Text

Public Class CountdownWP
    Inherits Microsoft.SharePoint.WebPartPages.WebPart
    Private _error As Boolean = False
    Private _CountdownDateTime As DateTime = Nothing
    Private _infoLink As String = Nothing
    Private _infoText As String = Nothing
    Private _CountdownDesc As String = Nothing
    Private _showDays As String
    Private _showHours As String
    Private _showMins As String
    Private _showSecs As String
    Private _web_url As String = Nothing

    <Personalizable(PersonalizationScope.Shared)> _
    <WebBrowsable(True)> _
    <System.ComponentModel.Category("My Property Group")> _
    <WebDisplayName("CountdownDateTime")> _
    <WebDescription("CountdownDateTime Property")> _
    Public Property CountdownDateTime() As DateTime
        Get
            If _CountdownDateTime = Nothing Then
                _CountdownDateTime = Now
            End If
            Return _CountdownDateTime
        End Get
        Set(ByVal value As DateTime)
            _CountdownDateTime = value
        End Set
    End Property

    <Personalizable(PersonalizationScope.Shared)> _
    <WebBrowsable(True)> _
    <System.ComponentModel.Category("My Property Group")> _
    <WebDisplayName("infoLink")> _
    <WebDescription("infoLink Property")> _
    Public Property infoLink() As String
        Get
            If _infoLink = Nothing Then
                _infoLink = ""
            End If
            Return _infoLink
        End Get
        Set(ByVal value As String)
            _infoLink = value
        End Set
    End Property

    <Personalizable(PersonalizationScope.Shared)> _
    <WebBrowsable(True)> _
    <System.ComponentModel.Category("My Property Group")> _
    <WebDisplayName("infoText")> _
    <WebDescription("infoText Property")> _
    Public Property infoText() As String
        Get
            If _infoText = Nothing Then
                _infoText = ""
            End If
            Return _infoText
        End Get
        Set(ByVal value As String)
            _infoText = value
        End Set
    End Property

    <Personalizable(PersonalizationScope.Shared)> _
    <WebBrowsable(True)> _
    <System.ComponentModel.Category("My Property Group")> _
    <WebDisplayName("CountdownDesc")> _
    <WebDescription("CountdownDesc Property")> _
    Public Property CountdownDesc() As String
        Get
            If _CountdownDesc = Nothing Then
                _CountdownDesc = ""
            End If
            Return _CountdownDesc
        End Get
        Set(ByVal value As String)
            _CountdownDesc = value
        End Set
    End Property

    <Personalizable(PersonalizationScope.Shared)> _
    <WebBrowsable(True)> _
    <System.ComponentModel.Category("My Property Group")> _
    <WebDisplayName("showDays")> _
    <WebDescription("showDays Property")> _
    Public Property showDays() As String
        Get
            If _showDays = Nothing Then
                _showDays = ""
            End If
            Return _showDays
        End Get
        Set(ByVal value As String)
            _showDays = value
        End Set
    End Property

    <Personalizable(PersonalizationScope.Shared)> _
    <WebBrowsable(True)> _
    <System.ComponentModel.Category("My Property Group")> _
    <WebDisplayName("showHours")> _
    <WebDescription("showHours Property")> _
    Public Property showHours() As String
        Get
            If _showHours = Nothing Then
                _showHours = ""
            End If
            Return _showHours
        End Get
        Set(ByVal value As String)
            _showHours = value
        End Set
    End Property

    <Personalizable(PersonalizationScope.Shared)> _
    <WebBrowsable(True)> _
    <System.ComponentModel.Category("My Property Group")> _
    <WebDisplayName("showMins")> _
    <WebDescription("showMins Property")> _
    Public Property showMins() As String
        Get
            If _showMins = Nothing Then
                _showMins = ""
            End If
            Return _showMins
        End Get
        Set(ByVal value As String)
            _showMins = value
        End Set
    End Property

    <Personalizable(PersonalizationScope.Shared)> _
    <WebBrowsable(True)> _
    <System.ComponentModel.Category("My Property Group")> _
    <WebDisplayName("showSecs")> _
    <WebDescription("showSecs Property")> _
    Public Property showSecs() As String
        Get
            If _showSecs = Nothing Then
                _showSecs = ""
            End If
            Return _showSecs
        End Get
        Set(ByVal value As String)
            _showSecs = value
        End Set
    End Property


    Public Sub New()
        Me.ExportMode = WebPartExportMode.All
    End Sub

    Public Overrides Function GetToolParts() As ToolPart()
        Dim toolParts(2) As ToolPart
        Dim wptp As WebPartToolPart = New WebPartToolPart
        Dim custom As CustomPropertyToolPart = New CustomPropertyToolPart
        toolParts(0) = wptp
        toolParts(1) = New CountdownTP
        Return toolParts
    End Function



    ''' <summary>
    ''' Create all your controls here for rendering.
    ''' Try to avoid using the RenderWebPart() method.
    ''' </summary>
    Protected Overloads Overrides Sub CreateChildControls()
        If Not _error Then
            Try

                MyBase.CreateChildControls()

                Dim _web As SPWeb = SPContext.Current.Web
                _web_url = _web.ServerRelativeUrl

                Dim dDiff As TimeSpan = CountdownDateTime.Subtract(Now)
                Dim days As Integer = dDiff.Days
                Dim hours As Integer = dDiff.Hours
                Dim mins As Integer = dDiff.Minutes
                Dim secs As Integer = dDiff.Seconds

                'Dim sb As New StringBuilder
                'sb.Append("Date " & CountdownDateTime & "<br />")
                'sb.Append("Days: " & days & " Hours: " & hours & " Mins: " & mins & " Secs: " & secs & "<br />")
                'sb.Append("Days: " & showDays & " Hours: " & showHours & " Mins: " & showMins & " Secs: " & showSecs)
                'Me.Controls.Add(New LiteralControl(sb.ToString))

                Me.Controls.Add(New LiteralControl("<p>"))

                If showDays = "True" Then
                    get_images(days, _web_url)
                    Dim img As New Web.UI.WebControls.Image
                    img.ImageUrl = _web_url & "/lists/countdownimagelibrary/days.jpg"
                    img.AlternateText = "Days"
                    Me.Controls.Add(img)
                    Me.Controls.Add(New LiteralControl("<br/>"))
                End If

                If showHours = "True" Then
                    get_images(hours, _web_url)
                    Dim img As New Web.UI.WebControls.Image
                    img.ImageUrl = _web_url & "/lists/countdownimagelibrary/Hours.jpg"
                    img.AlternateText = "Hours"
                    Me.Controls.Add(img)
                    Me.Controls.Add(New LiteralControl("<br/>"))
                End If



                If showMins = "True" Then
                    get_images(mins, _web_url)
                    Dim img As New Web.UI.WebControls.Image
                    img.ImageUrl = _web_url & "/lists/countdownimagelibrary/Mins.jpg"
                    img.AlternateText = "Mins"
                    Me.Controls.Add(img)
                    Me.Controls.Add(New LiteralControl("<br/>"))
                End If


                If showSecs = "True" Then
                    get_images(secs, _web_url)
                    Dim img As New Web.UI.WebControls.Image
                    img.ImageUrl = _web_url & "/lists/countdownimagelibrary/Secs.jpg"
                    img.AlternateText = "Secs"
                    Me.Controls.Add(img)
                    Me.Controls.Add(New LiteralControl("<br/>"))
                End If

                If Not showDays = "True" And Not showHours = "True" And Not showMins = "True" And Not showSecs = "True" Then

                    Me.Controls.Add(New LiteralControl("Please open the toolpane to enter a date to countdown to."))

                End If

                Me.Controls.Add(New LiteralControl("</p>"))

                If Not CountdownDesc = Nothing Then
                    Me.Controls.Add(New LiteralControl("<p>"))
                    Me.Controls.Add(New LiteralControl(CountdownDesc))
                    Me.Controls.Add(New LiteralControl("</p>"))
                End If

                If Not infoLink = Nothing Then
                    Me.Controls.Add(New LiteralControl("<p>"))
                    Dim link As New Web.UI.WebControls.HyperLink
                    link.NavigateUrl = infoLink
                    If Not infoText = Nothing Then

                        link.Text = infoText
                    Else
                        link.Text = infoLink
                    End If
                    Me.Controls.Add(link)
                    Me.Controls.Add(New LiteralControl("</p>"))
                End If


                ' Your code here...
                'Me.Controls.Add(New LiteralControl(sb.ToString))
            Catch ex As Exception
                HandleException(ex)
            End Try
        End If
    End Sub


    Sub get_images(ByVal number As Integer, ByVal web_url As String)

        Dim strNumber As String

        If number < 0 Then
            strNumber = 0
        Else
            strNumber = number
        End If

        Dim i As Integer = 0
        Dim sb As New StringBuilder
        Dim img As Web.UI.WebControls.Image = Nothing


        Select Case strNumber.Length

            Case 1
                img = New Web.UI.WebControls.Image
                img.ImageUrl = web_url & "/lists/countdownimagelibrary/blank.jpg"
                img.AlternateText = ""
                Me.Controls.Add(img)
                img = New Web.UI.WebControls.Image
                img.ImageUrl = web_url & "/lists/countdownimagelibrary/blank.jpg"
                img.AlternateText = ""
                Me.Controls.Add(img)

                For i = 0 To strNumber.Length - 1
                    img = New Web.UI.WebControls.Image
                    img.ImageUrl = web_url & "/lists/countdownimagelibrary/" & strNumber.Substring(i, 1) & ".jpg"
                    img.AlternateText = strNumber.Substring(i, 1)
                    Me.Controls.Add(img)
                Next

            Case 2
                img = New Web.UI.WebControls.Image
                img.ImageUrl = web_url & "/lists/countdownimagelibrary/blank.jpg"
                img.AlternateText = ""
                Me.Controls.Add(img)

                For i = 0 To strNumber.Length - 1
                    img = New Web.UI.WebControls.Image
                    img.ImageUrl = web_url & "/lists/countdownimagelibrary/" & strNumber.Substring(i, 1) & ".jpg"
                    img.AlternateText = strNumber.Substring(i, 1)
                    Me.Controls.Add(img)
                Next

            Case Else

                For i = 0 To strNumber.Length - 1
                    img = New Web.UI.WebControls.Image
                    img.ImageUrl = web_url & "/lists/countdownimagelibrary/" & strNumber.Substring(i, 1) & ".jpg"
                    img.AlternateText = strNumber.Substring(i, 1)
                    Me.Controls.Add(img)
                Next


        End Select


    End Sub


    ''' <summary>
    ''' Ensures that the CreateChildControls() is called before events.
    ''' Use CreateChildControls() to create your controls.
    ''' </summary>
    ''' <param name="e"></param>
    Protected Overloads Overrides Sub OnLoad(ByVal e As EventArgs)
        If Not _error Then
            Try
                MyBase.OnLoad(e)

                ' Your code here...
                Me.EnsureChildControls()
            Catch ex As Exception
                HandleException(ex)
            End Try
        End If
    End Sub

    ''' <summary>
    ''' Clear all child controls and add an error message for display.
    ''' </summary>
    ''' <param name="ex"></param>
    Private Sub HandleException(ByVal ex As Exception)
        Me._error = True
        Me.Controls.Clear()
        Me.Controls.Add(New LiteralControl(ex.Message))
    End Sub
End Class
